local SuperRankData = class("SuperRankData")
local GuildBaseInfo = require("app.public.dao.GuildBaseInfo")
local UserBaseInfo = require("app.public.dao.UserBaseInfo")
local RankDataConvertHelper = require("app.widget.rank.RankDataConvertHelper")


function SuperRankData:ctor()

end

function SuperRankData:C2S_GetCrossEliteHistoryInfo(playType)
    -- print("================>>> C2S_GetCrossEliteHistoryInfo  playType ", playType)
    app.session:request("pb_rank.C2S_GetCrossEliteHistoryInfo",{playType = playType},function(msg)
        -- dump(msg, "C2S_GetCrossEliteHistoryInfo", 5)
        if Enums.TeamTarget.eliteMatchArena == playType then
            local d = {}
            for k, v in pairs(rawget(msg, "lastSeasons") or {}) do
                table.insert(d, {name = string.format(L("SuperRankSeasonStr"), v), season = v, tp = Enums.SuperRankMenu.CSArena})
            end
            table.sort(d, function(a, b)
                return a.season > b.season
            end)
            EM:notify(ViewEvent.SuperRank_ReloadMenu, d, Enums.SuperRankMenu.CSArena)
        elseif Enums.TeamTarget.eliteMatchDuel == playType then
            local d = {}
            for k, v in pairs(rawget(msg, "lastSeasons") or {}) do
                table.insert(d, {name = string.format(L("SuperRankSeasonStr"), v), season = v, tp = Enums.SuperRankMenu.CSDuel})
            end
            table.sort(d, function(a, b)
                return a.season > b.season
            end)
            EM:notify(ViewEvent.SuperRank_ReloadMenu, d, Enums.SuperRankMenu.CSDuel)
        end
	end)
end

function SuperRankData:C2S_GetCrossGuildHistoryInfo()
    -- print("================>>> C2S_GetCrossGuildHistoryInfo ")
    app.session:request("pb_rank.C2S_GetCrossGuildHistoryInfo",{},function(msg)
        -- dump(msg, "C2S_GetCrossGuildHistoryInfo", 5)
        local d = {}
        for k, v in pairs(rawget(msg, "lastSeasons") or {}) do
            table.insert(d, {name = string.format(L("SuperRankSeasonStr"), v), season = v, tp = Enums.SuperRankMenu.CSGuildWar})
        end
        table.sort(d, function(a, b)
            return a.season > b.season
        end)


        EM:notify(ViewEvent.SuperRank_ReloadMenu, d, Enums.SuperRankMenu.CSGuildWar)
	end)
end

function SuperRankData:C2S_GetCrossEliteHistoryRank(playType, seasonID)
    app.session:request("pb_rank.C2S_GetCrossEliteHistoryRank",{playType = playType, seasonID = seasonID},function(msg)

        -- dump(msg, "C2S_GetCrossEliteHistoryRank", 5)
        local avatars = {}
        local convertType
        local avatarNum
        if Enums.TeamTarget.eliteMatchDuel == playType then
            avatarNum  = tonumber(GD:getSystemPar( "elitematchRankDuel",3)) or 3
            convertType = RankDataConvertHelper.DataType.CSDuel
        elseif  Enums.TeamTarget.eliteMatchArena == playType then
            avatarNum  = tonumber(GD:getSystemPar( "elitematchRankArena",3)) or 3
            convertType = RankDataConvertHelper.DataType.CSArena
        else
            return
        end

        local rankList = rawget(msg, "rankList") or {}
        local startPageNum = avatarNum/3

        local totalNum = #rankList
        local leftNum = totalNum - avatarNum
        if leftNum < 0 then
            leftNum = 0 
            avatarNum = totalNum
        end

        local myInfo = nil
        local myInfoStr = nil
        local myId = UD.player.uid
        for k, v in pairs(rankList) do
            if k <= avatarNum then
                local t = nil
                local userInfo = rawget(v, "user")
                if userInfo then
                    t = {}
                    local baseUserInfo = UserBaseInfo.new()
                    baseUserInfo:read(userInfo)
                    t.baseUserInfo = baseUserInfo
                    t.rank = k
                    t.value1 = baseUserInfo.name
                    t.value2 = "<"..RankDataConvertHelper.getServerNameByServerIndex(baseUserInfo.serverID)..">"
                    t.value3 = L("rankcondition_20001")..": "..(rawget(v, "value") or 0)
                end
                avatars[k] = t    
            end
            if not myInfo then
                local userInfo = rawget(v, "user")
                if userInfo and userInfo.uid == myId then
                    myInfo = {
                        rank = k,
                        value = rawget(v, "value")
                    }
                    myInfoStr = RankDataConvertHelper.convertToBottomStr1(rawget(v, "value"), k)
                end
            end
            if myInfo and  k > avatarNum then
                break
            end
        end
        
        local avatarPageNum = math.ceil( avatarNum/3 )
        local pageNum = avatarPageNum + RankDataConvertHelper.getPageNumByNum(leftNum)

      
        local datas = {
            pageNum = pageNum,
            avatarPageNum = avatarPageNum,
            avatars = avatars,
            bottomInfo = RankDataConvertHelper.convertToBottomInfo(myInfoStr, pageNum, myInfo),
            rankData = RankDataConvertHelper.ConvertToData(convertType, rankList, {startRank = avatarNum + 1, startPage = avatarPageNum + 1})
        }
        EM:notify(ViewEvent.SuperRank_RefreshContent, datas)
	end)
end



function SuperRankData:C2S_GetCrossGuildHistoryRank(seasonID)
    app.session:request("pb_rank.C2S_GetCrossGuildHistoryRank",{seasonID = seasonID},function(msg)
        -- dump(msg, "C2S_GetCrossGuildHistoryRank", 5)
        local avatars = {}
        local avatarNum  = tonumber(GD:getSystemPar( "elitematchRankGuild",3)) or 3
        local rankList = rawget(msg, "rankList") or {}

        local startPageNum = avatarNum/3

        local totalNum = #rankList
        local leftNum = totalNum - avatarNum
        if leftNum < 0 then
            avatarNum = totalNum
            leftNum = 0 
        end

        local myInfo = nil
        local myInfoStr = nil
        local myId = UD:getGuildId()

        for k, v in pairs(rankList) do
            if k <= avatarNum then
                local t = nil
                local userInfo = rawget(v, "user")
                local guildData = rawget(v, "guild")
                if userInfo and guildData then
                    t = {}
                    local guildInfo = GuildBaseInfo.new()
                    guildInfo:read(guildData)
                    t.guildInfo = guildInfo
                    local baseUserInfo = UserBaseInfo.new()
                    baseUserInfo:read(userInfo)
                    t.baseUserInfo = baseUserInfo
                    t.rank = k
                    t.value1 = guildInfo:getGuildName()
                    t.value2 = "<"..guildInfo:getServerName()..">"
                    t.value3 = baseUserInfo.name 
                end
                avatars[k] = t
            end

            if not myInfo then
                local guildData = rawget(v, "guild")
                if guildData and guildData.uuid == myId then
                    myInfo = {
                        rank = k,
                        value = rawget(v, "value")
                    }
                    myInfoStr = RankDataConvertHelper.convertToBottomStr1(k, k)
                end
            end

            if myInfo and  k > avatarNum then
                break
            end
        end
        local avatarPageNum = math.ceil( avatarNum/3 )
        local pageNum = avatarPageNum + RankDataConvertHelper.getPageNumByNum(leftNum)

        local datas = {
            pageNum = pageNum,
            avatarPageNum = avatarPageNum,
            avatars = avatars,
            bottomInfo = RankDataConvertHelper.convertToBottomInfo(myInfoStr, pageNum, myInfo),
            rankData = RankDataConvertHelper.ConvertToData(RankDataConvertHelper.DataType.CSGuildWar, rankList, {startRank = avatarNum + 1, startPage = avatarPageNum + 1})
        }
        EM:notify(ViewEvent.SuperRank_RefreshContent, datas)
	end)
end







return SuperRankData